package cn.edu.dgut.css.springboottestthree.dao;

import cn.edu.dgut.css.springboottestthree.entity.Region;
import cn.edu.dgut.css.springboottestthree.properties.RegionProperties;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Repository;
import org.springframework.web.client.RestTemplate;

import javax.annotation.PostConstruct;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @date: 2020/11/14 22:44
 * @author: ZkhEight
 */
@Repository
public class RegionDao {

    private List<Region> regions = new ArrayList<>();


    @Autowired
    private RegionProperties regionProperties;

    @javax.annotation.Resource
    private RestTemplate restTemplate;

    public List<Region> getRegions() {
        return regions;
    }

    @PostConstruct
    public List<Region> getData() throws IOException {

        RequestEntity<Void> requestEntity = RequestEntity.get(URI.create(regionProperties.getUrl()))
                .headers(httpHeaders -> httpHeaders.add("User-Agent", "Mozilla/5.0"))
                .build();
        ResponseEntity<Resource> responseEntity = restTemplate.exchange(requestEntity, Resource.class);
        Resource resource = responseEntity.getBody();
        assert resource != null;
        Reader in = new InputStreamReader(resource.getInputStream(), StandardCharsets.UTF_8);
        Iterable<CSVRecord> records = CSVFormat.RFC4180.parse(in);

        boolean isHeader = true;
        List<String> datelist = new ArrayList<>();  //获取表头（每天的日期）
        int id = 0;
        for (CSVRecord record : records) {
            //获取表头
            if (isHeader) {
                isHeader = false;
                //从第5列开始是日期/病例
                for (int i = record.size() - 1; i >= 4; i--) {
                    String[] split = record.get(i).split("/");
                    datelist.add("20" + split[2] + "年" + split[0] + "月" + split[1] + "日");
                }
            } else {
                //获取数据
                Region region = new Region();
                Map<String, Long> map = new LinkedHashMap<>();
                //从第5列开始是日期/病例
                for (int i = record.size() - 1, j = 0; i >= 4; i--, j++) {
                    map.put(datelist.get(j), Long.valueOf(record.get(i)));
                }
                region.setId(id++);
                region.setData(map);
                region.setProvinceName(record.get(0));
                region.setCountryName(record.get(1));
                regions.add(region);
            }
        }
//        System.out.println(regions);
        return regions;
    }


}
